From 2e909720040d1598aee4b7164ea0b072157fad5b Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 11 Jul 2014 13:34:50 -0700 Subject: [PATCH] Do not run examples during 'cargo test' --- src/bin/cargo-test.rs | 21 +++------------------ src/cargo/ops/cargo_compile.rs | 15 +++++++++++++-- tests/test_cargo_test.rs | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/bin/cargo-test.rs b/src/bin/cargo-test.rs index b0e2a41a4..bee0541ce 100644 --- a/src/bin/cargo-test.rs +++ b/src/bin/cargo-test.rs @@ -9,7 +9,6 @@ extern crate serialize; extern crate hammer; use std::os; -use std::io::{UserExecute, fs}; use cargo::ops; use cargo::{execute_main_without_stdin}; @@ -53,31 +52,17 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { target: None, }; - try!(ops::compile(&root, compile_opts).map(|_| None::<()>).map_err(|err| { + let test_executables = try!(ops::compile(&root, compile_opts).map_err(|err| { CliError::from_boxed(err, 101) })); let test_dir = root.dir_path().join("target").join("test"); - let mut walk = try!(fs::walk_dir(&test_dir).map_err(|e| { - CliError::from_error(e, 1) - })); - - for file in walk { - // TODO: The proper fix is to have target knows its expected - // output and only run expected executables. - if file.display().to_string().as_slice().contains("dSYM") { continue; } - if !is_executable(&file) { continue; } - - try!(util::process(file).exec().map_err(|e| { + for file in test_executables.iter() { + try!(util::process(test_dir.join(file.as_slice())).exec().map_err(|e| { CliError::from_boxed(e.box_error(), 1) })); } Ok(None) } - -fn is_executable(path: &Path) -> bool { - if !path.is_file() { return false; } - path.stat().map(|stat| stat.perm.intersects(UserExecute)).unwrap_or(false) -} diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 427616b9d..9fdf73b20 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -38,7 +38,7 @@ pub struct CompileOptions<'a> { pub target: Option<&'a str>, } -pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()> { +pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult> { let CompileOptions { update, env, shell, jobs, target } = options; let target = target.map(|s| s.to_string()); @@ -88,7 +88,18 @@ pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()> try!(ops::compile_targets(env.as_slice(), targets.as_slice(), &package, &PackageSet::new(packages.as_slice()), &resolve, &mut config)); - Ok(()) + let test_executables: Vec = targets.iter() + .filter_map(|target| { + if target.get_profile().is_test() { + debug!("Run Target: {}", target.get_name()); + Some(target.get_name().to_string()) + } else { + debug!("Skip Target: {}", target.get_name()); + None + } + }).collect(); + + Ok(test_executables) } fn source_ids_from_config() -> CargoResult> { diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index ed2136ab5..85129fa1f 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -222,3 +222,20 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured"; assert!(out == format!("{}\n\n{}\n\n\n{}\n\n", head, internal, external).as_slice() || out == format!("{}\n\n{}\n\n\n{}\n\n", head, external, internal).as_slice()); }) + +test!(dont_run_examples { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + "#) + .file("examples/dont-run-me-i-will-fail.rs", r#" + fn main() { fail!("Examples should not be run by 'cargo test'"); } + "#); + assert_that(p.cargo_process("cargo-test"), + execs().with_status(0)); +}) -- 2.30.2